﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using FCNS.Data.fcnsDataSetTableAdapters;
using System.Data;

namespace ReportS
{
    /// <summary>
    /// PfMaoLi.xaml 的交互逻辑
    /// </summary>
    public partial class PfMaoLi : Window
    {
        public PfMaoLi()
        {
            InitializeComponent();

            InitVar();
            InitEvent();
        }

        private void InitVar()
        {
            uCSearchOrder1.Init(DoubleH.Utility.DataTableText.销售毛利表);
            dataGridExt1.FormatLastRow = true;
        }

        private void InitEvent()
        {
            buttonSearch.Click += (ss, ee) =>
            {
                double quantity = 0, money = 0, average = 0, maoli = 0;
                List<ClassA> obj=new List<ClassA>();
                foreach(Table.ProductSIO io in uCSearchOrder1.GetSearchData())
                {
                    Table.ProductS ps = MainWindow.allProductS.Find(f => {return  f.Id == io.ProductSId; });
                    if (ps == null)
                        continue;

                    ClassA ca = new ClassA();
                    ca.ProductSIdName = ps.Name;
                    ca.Unit = ps._UnitName;
                    ca.Standard = ps.Standard;
                    ca.Code = ps.Code;

                    ca.Quantity = io.Quantity;
                    ca.Money = io.Quantity * io.WholesalePrice;
                    ca.Average = io.Quantity * io.AveragePrice;
                    ca.MaoLi = ca.Money - ca.Average;
                    ca.MaoLiLv =( ca.MaoLi / ca.Money).ToString("p"+Table.SysConfig.SysConfigParams.DecimalPlaces);
                    ca.StoreSIdName = MainWindow.allStoreS.Find(f => { return f.Id == io.StoreSId; }).Name;
                    obj.Add(ca);

                    quantity += ca.Quantity;
                    money += ca.Money;
                    average += ca.Average;
                    maoli += ca.MaoLi;
                }
                if(obj.Count>0)
                obj.Add(new ClassA() {ProductSIdName="合计",Money=money,Quantity=quantity,Average=average,MaoLi=maoli });

                dataGridExt1.ItemsSource=obj;
            };
        }
        
    }

    public class ClassA
    {
        public string ProductSIdName { get; set; }
        public string Code { get; set; }
        public string Unit { get; set; }
        public string Standard { get; set; }
        public double Quantity { get; set; }
        public double Money { get; set; }
        public double Average { get; set; }
        public double MaoLi { get; set; }
        public string MaoLiLv { get; set; }
        public string StoreSIdName { get; set; }
    }
}
